using System;
using System.Collections.Generic;
using SCG.SolarVengeanceEngine;
using SCG.SolarVengeanceEngine.BrainInfo;

namespace SCG.SolarVengeance.Brains
{
    public class Juggernaut : Brain
    {
        public override ChallengeLevel Challenge
        {
            get
            {
                return ChallengeLevel.Easy;
            }
        }

        //Establish the ships that the brain can build
        public override List<StarShipType> AvailableShipTypes
        {
            get
            {
                List<StarShipType> shipTypes = new List<StarShipType>();
                shipTypes.Add(StarShipType.WarShip);
                shipTypes.Add(StarShipType.ScanShip);
                shipTypes.Add(StarShipType.Drone);
                shipTypes.Add(StarShipType.EMine);
                return shipTypes;
            }
        }
        //Execute brain's logic
        public override void Execute()
        {
            //Find our capital
            if (_capital == null)
                foreach(StarSystemInfo system in StarSystems)
                    if (system.OwnedByMe && system.IsCapital)
                    {
                        _capital = system;
                        break;
                    }

            //Count StarSystem values
            int sysValue = 0;
            foreach (StarSystemInfo sys in StarSystems)
                if (sys.OwnedByMe)
                    if (!sys.IsCapital)
                        sysValue += sys.Value;                

            //Give orders to StarSystems
            foreach(StarSystemInfo sys in StarSystems)
                if (sys.OwnedByMe)
                {
                    //Keep up shields
                    if (sys.Shields < sys.Value / 2)
                        BuildShields(sys, 3);

                    //Special capital defense logic
                    if (sys.IsCapital && sysValue > 40)
                    {
                        if (Random(1, 10) > 7)
                            BuildStarShip(sys, StarShipType.EMine, 1, 0, false);
                        BuildStarShip(sys, StarShipType.WarShip, 16, Random(5, 10), false);
                    }
                    else
                    {
                        //Build as many ships as we can (try building 8)
                        StarSystemInfo visibleTarget = ClosestStarSystem(sys.X, sys.Y, Int32.MaxValue, FindSystemFlags.VisibleUnfriendly);
                        if (visibleTarget != null && visibleTarget.Shields < 6)
                        {
                            for (int i = 0; i < 8; i++)
                                BuildStarShip(sys, StarShipType.WarShip, Random(5, 15), visibleTarget.Shields + 1, false);
                        }
                        else
                        {
                            for (int i = 0; i < 8; i++)
                                BuildStarShip(sys, StarShipType.WarShip, Random(5, 15), Random(2, 8), false);
                        }
                    }
                }

            //Order StarShips to attack the target!
            foreach (StarShipInfo ship in MyStarShips)
            {
                if (ship.ShipType == StarShipType.EMine)
                    Move(ship, ship.X + Random(-4, 4), ship.Y + Random(-4, 4));
                else if (ship.Engines >= 16)
                    Defend(ship, _capital);
                else
                {
                    StarSystemInfo target = ClosestStarSystem(ship.X, ship.Y, Int32.MaxValue, FindSystemFlags.Unfriendly);
                    Target(ship, target);
                }
            }
        }

        //Provide a description
        public override string Description
        {
            get
            {
                return "Juggernaut is an especially aggressive opponent.  It assaults target StarSystems with waves of WarShips in a concentrated attack.";
            }
        }

        //private members
        private StarSystemInfo _capital = null;
    }
}
